import jwtDecode from 'jwt-decode'
import Cookie from 'js-cookie'

export const state = () => ({
  token: '',
  authUser: null,
  currentRoute: '',
  showRegisterForm: false,
  showLoginForm: false,
  loginUserName: '18311109710',
  loginUserPassword: 'admin',
  category: '',
  findRouteParam: '',
  myRouteParam: '',
  contentModal: false,
  resize: 0
})

export const mutations = {
  SET_AUTH_USER: function (state, authUser) {
    state.authUser = authUser
  },
  SET_RESIZE: function (state, resize) {
    state.resize = resize
  },
  SET_CONTENT_MODAL: function (state, contentModal) {
    state.contentModal = contentModal
  },
  SET_CATEGORY_DATA: function (state, categoryData) {
    state.categoryData = categoryData
  },
  SET_FIND_ROUTEPARAM: function (state, findRouteParam) {
    state.findRouteParam = findRouteParam
  },
  SET_MY_ROUTEPARAM: function (state, myRouteParam) {
    state.myRouteParam = myRouteParam
  },
  SET_CATEGORY: function (state, category) {
    state.category = category
  },
  SET_CURRENT_ROUTE: function (state, routeName) {
    state.currentRoute = routeName
  },
  SET_SHOW_LOGIN_FORM: function (state, showLoginForm) {
    state.showLoginForm = showLoginForm
  },
  SET_SHOW_REGISTER_FORM: function (state, showRegisterForm) {
    state.showRegisterForm = showRegisterForm
  },
  SET_LOGIN_USER_NAME: function (state, loginUserName) {
    state.loginUserName = loginUserName
  },
  SET_LOGIN_USER_PASSWORD: function (state, loginUserPassword) {
    state.loginUserPassword = loginUserPassword
  }
}

export const actions = {
  async login ({commit, dispatch}, {username, password}) {
    await this.$axios.post('/auth/login', {
      clientId: '098f6bcd4621d373cade4e832627b4f6',
      password: password,
      userName: username
    }).then(function (data) {
      if (data.data.code === 0) {
        Cookie.set('access_token', data.data.data.access_token, data.data.data.expires_in * 1000)
        Cookie.set('user', jwtDecode(data.data.data.access_token).user, data.data.data.expires_in * 1000)
        commit('SET_SHOW_LOGIN_FORM', false)
        commit('SET_AUTH_USER', jwtDecode(data.data.data.access_token).user)
        dispatch('content/getCategoryData')
      } else {
        console.log(data.data.message)
      }
    }).catch(function (err) {
      console.log(err)
    })
  },
  async logout ({commit}) {
  },
  setResize ({commit}, {resize}) {
    commit('SET_RESIZE', resize)
  },
  setCurrentRoute ({commit}, {currentRoute}) {
    commit('SET_CURRENT_ROUTE', currentRoute)
  },
  setAuthUser ({commit}, {authUser}) {
    commit('SET_AUTH_USER', authUser)
  },
  setContentModal ({commit}, {contentModal}) {
    commit('SET_CONTENT_MODAL', contentModal)
  },
  setFindRouteParam ({commit}, {findRouteParam}) {
    commit('SET_FIND_ROUTEPARAM', findRouteParam)
  },
  setMyRouteParam ({commit}, {myRouteParam}) {
    commit('SET_MY_ROUTEPARAM', myRouteParam)
  },
  setCategory ({commit}, {category}) {
    commit('SET_CATEGORY', category)
  },
  setShowLoginForm ({commit}, {showLoginForm}) {
    commit('SET_SHOW_LOGIN_FORM', showLoginForm)
  },
  setShowRegisterForm ({commit}, {showLoginForm}) {
    commit('SET_SHOW_REGISTER_FORM', showLoginForm)
  },
  setLoginUserName ({commit}, {loginUserName}) {
    commit('SET_LOGIN_USER_NAME', loginUserName)
  },
  setLoginUserPassword ({commit}, {loginUserPassword}) {
    commit('SET_LOGIN_USER_PASSWORD', loginUserPassword)
  }

}
